package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.credit_bank.pojo.PostComments;
import csu.web.credit_bank.pojo.dto.CommentCreateDto;
import csu.web.credit_bank.service.PostCommentsService;
import csu.web.credit_bank.mapper.PostCommentsMapper;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 于欣淇
* @description 针对表【post_comments(论坛评论内容表)】的数据库操作Service实现
* @createDate 2025-06-29 21:14:52
*/
@Service
public class PostCommentsServiceImpl extends ServiceImpl<PostCommentsMapper, PostComments>
    implements PostCommentsService {

    @Autowired
    private UUIDManager uuidManager;

    @Override
    public List<PostComments> getCommentsByPostId(String postId) {
        QueryWrapper<PostComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", postId);
        queryWrapper.orderByAsc("create_time");
        return this.list(queryWrapper);
    }

    @Override
    public PostComments createComment(CommentCreateDto commentDto) {
        uuidManager.generateUUID(1000);
        PostComments newComment = new PostComments();
        newComment.setId(uuidManager.getUUID());
        newComment.setPostId(commentDto.getPostId());
        newComment.setContent(commentDto.getContent());
        newComment.setCommenterId(commentDto.getContenter());
        newComment.setCreateTime(new Date());
        newComment.setLikes(0);
        this.save(newComment);
        return newComment;
    }

    public List<PostComments> getCommentsByArticleId(String articleId) {
        QueryWrapper<PostComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", articleId);
        return this.list(queryWrapper);
    }
}




